//
//  import.cpp
//  
//
//  Created by Nikita Luzyanin on 30.11.13.
//
//

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include "functions.cpp"
using namespace std;

int main(int argc, char* argv[])
{
    char *pathToFS;
    
    pathToFS = argv[1];
    FILE *f;
    f = fopen(pathToFS, "rb");
    if (f == NULL) {
        cout << "Path to FS does not exist\n";
        return 1;
    }
    fclose(f);
    
    char pathToRoot[1000];
    strcpy(pathToRoot, pathToFS);
    strcat(pathToRoot, "/0");
    f = fopen(pathToRoot, "rb");
    if (f == NULL) {
        cout << "FS is not formatted\n";
        return 1;
    }
    fclose(f);
    
    char pathToFile[1000];
    strcpy(pathToFile, argv[2]);
    
    f = fopen(pathToFile, "rb");
    if (f == NULL) {
        cout << "File on host does not exist\n";
        return 1;
    }
    fseek(f,0,SEEK_END);
    long size = ftell(f);
    fclose(f);
    
    char pathInFS[1000];
    strcpy(pathInFS, argv[3]);
    char metaBlock[10];
    if (findMetaBlock(pathToFS, metaBlock, pathInFS, true, false, true) != 0) {
        cout << "Mistake\n";
        return 1;
    }
    char contentBlock[10];
    if (findContentBlock(pathToFS, metaBlock, contentBlock) != 0) {
        cout << "Mistake\n";
        return 1;
    }
    char pathToContentBlock[1000], pathToMetaBlock[1000];
    strcpy(pathToContentBlock, pathToFS);
    strcat(pathToContentBlock, contentBlock);
    
    int sizeOfBlock;
    fstream config;
    config.open("config");
    config >> sizeOfBlock;
    config.close();
    
    int additionalNumberOfBlocks = size/sizeOfBlock;
    int blocksAdded = 0;
    fstream HF, FSF;
    
    char ch, nextContentBlock[10];
    if (clearContentBlocks(pathToFS, metaBlock, contentBlock, true) != 0) {
        cout << "Mistake!\n";
        return 1;
    }
    int bytesWritten = 0;
    HF.open(pathToFile);
    FSF.open(pathToContentBlock);
    if (additionalNumberOfBlocks > 0) {
        if (findFreeBlock(pathToFS, nextContentBlock, true) != 0) {
            cout << "No more free blocks\n";
        }
        
        FSF << nextContentBlock;
        FSF << "\n";
        bytesWritten ++;
    } else {
        FSF >> nextContentBlock;
        int currentPositionFSF = FSF.tellg();
        FSF.seekp(currentPositionFSF + 1);
    }
    ch = HF.get();
    while (!HF.eof()) {
        if ((bytesWritten + strlen(nextContentBlock)) < ((sizeOfBlock) * (blocksAdded + 1))) {
            FSF << ch;
            bytesWritten ++;
            ch = HF.get();
        } else {
            FSF.close();
            strcpy(pathToContentBlock, pathToFS);
            strcat(pathToContentBlock, nextContentBlock);
            FSF.open(pathToContentBlock);
            if (additionalNumberOfBlocks > (blocksAdded + 1)) {
                if (findFreeBlock(pathToFS, nextContentBlock, true) != 0) {
                    cout << "No more free blocks\n";
                }
                FSF << nextContentBlock;
                FSF << "\n";
                bytesWritten = bytesWritten + 3;
            } else {
                FSF << "n\n";
                bytesWritten = bytesWritten + 2;
                strcpy(nextContentBlock, "n");
            }
            blocksAdded ++;
        }
    }
    FSF << " ";
    while ((bytesWritten + strlen(nextContentBlock)) < sizeOfBlock - 2) {
        FSF << "0";
        bytesWritten ++;
    }
    HF.close();
    FSF.close();
    
    changeSizeAndDate(pathToFS, metaBlock,size, false);
    
    return 0;
}